//
//  JWShopArticleInfoVC.m
//  jinwang
//
//  Created by jzb on 2018/4/11.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWShopArticleInfoVC.h"
#import "JWShopArticleInfoCell.h"
#import "JWShopAdvisoryModel.h"

@interface JWShopArticleInfoVC ()<UIWebViewDelegate>

@property (nonatomic,strong)JWShopArticleInfoCell *cell;
@property (nonatomic,strong)JWShopAdvisoryModel *model;
@property (nonatomic,strong)UIWebView *webView;

@end

@implementation JWShopArticleInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"资讯";
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-20, 1)];
    _webView.delegate = self;
    _webView.scrollView.scrollEnabled = NO;
    
   
    [self setUpTableView];
    [self makeUrl_mall_articleInfo];
}

//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
//    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
     self.tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
     self.tableView.rowHeight =UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWShopArticleInfoCell class])]];
   
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:[JWShopArticleInfoCell getCellReuseIdentifier]];
    
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
    self.cell.model = _model;
    
//    UIWebView *webView = [[UIWebView alloc]init];
//
//
//
//    NSString *js=@"var script = document.createElement('script');"
//    "script.type = 'text/javascript';"
//    "script.text = \"function ResizeImages() { "
//    "var myimg,oldwidth;"
//    "var maxwidth = %f;"
//    "for(i=0;i <document.images.length;i++){"
//    "myimg = document.images[i];"
//    "if(myimg.width > maxwidth){"
//    "oldwidth = myimg.width;"
//    "myimg.width = %f;"
//    "}"
//    "}"
//    "}\";"
//    "document.getElementsByTagName('head')[0].appendChild(script);";
//    js=[NSString stringWithFormat:js,self.cell.WebViewBGView.width,self.cell.WebViewBGView.width];
//    [webView stringByEvaluatingJavaScriptFromString:js];
//    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
//
////    _JiaWenWebView.frame = CGRectMake(0, 0, self.DetailsCell.imgsBGView.width, height);
////    self.DetailsCell.webViewHeight.constant = _JiaWenWebView.height;
//
//    UIScrollView *tempView = (UIScrollView *)[webView.subviews objectAtIndex:0];
//    tempView.scrollEnabled = NO;
//    [webView loadHTMLString:_model.article_content baseURL:nil];
//
//    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
//
//    NSLog(@"---=-=-=-=>>%lf",height);
//    webView.frame = CGRectMake(0, 0, self.cell.WebViewBGView.width, height);
    [self.cell.WebViewBGView addSubview:_webView];
    
    return self.cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    CGSize size = CGSizeMake(kScreenWidth - 20, MAXFLOAT);//设置高度宽度的最大限度
    CGRect article_titleRect = [_model.article_title boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]} context:nil];

    return 70+article_titleRect.size.height+_webView.height;

}

#pragma mark - UIWebView Delegate Methods
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //获取到webview的高度
    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue]+10;
    self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, kScreenWidth-20, height);
    [self.tableView reloadData];
}


-(void)makeUrl_mall_articleInfo{
    [[DDNetworkManagerDate makeUrl_mall_articleInfoWithtoken:k_user_token article_id:_article_id] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            NSLog(@"====>>%@",responseObject);
            
            NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            _model = [JWShopAdvisoryModel yy_modelWithDictionary:data];
            NSString *htmls = [NSString stringWithFormat:@"<html> \n"
                               "<head> \n"
                               "<style type=\"text/css\"> \n"
                               "body {font-size:15px;}\n"
                               "</style> \n"
                               "</head> \n"
                               "<body>"
                               "<script type='text/javascript'>"
                               "window.onload = function(){\n"
                               "var $img = document.getElementsByTagName('img');\n"
                               "for(var p in  $img){\n"
                               " $img[p].style.width = '100%%';\n"
                               "$img[p].style.height ='auto'\n"
                               "}\n"
                               "}"
                               "</script>%@"
                               "</body>"
                               "</html>",_model.article_content];
//            [_webView stringByEvaluatingJavaScriptFromString: script];
//            [_webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
            
            
            [_webView loadHTMLString:htmls baseURL:nil];
        }
        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
